//
//  CarBrandListViewController.m
//  AppointmentForCar
//
//  Created by Herron on 2016/11/10.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "CarBrandListViewController.h"
#import "BrandSeriesViewController.h"
#import "CarBrandCell.h"

@interface CarBrandListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *tableArray;

@end

@implementation CarBrandListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"品牌";
    self.tableArray = @[].mutableCopy;
    [self.tableView registerNib:[UINib nibWithNibName:@"CarBrandCell" bundle:nil] forCellReuseIdentifier:@"CarBrandCellID"];
    [HUDManager showLoadingHUDViewWithText:@"正在加载"];
    [NetWork postNetWorkWithUrl:kURLCarBrandList parame:nil successBlock:^(NSDictionary *dic) {
        [HUDManager hideHUDView];
        NSLog(@"车辆品牌 = %@", dic);
        NSArray *brandList = dic[@"data"][@"provinceList"];
        if (brandList.count) {
            self.tableArray = [CarBrandModel arrayOfModelsFromDictionaries:brandList error:nil];
            [self.tableView reloadData];
        } else {
            [HUDManager showWarningWithText:@"暂无数据"];
        }
    } errorBlock:^(id error) {
        [HUDManager showWarningWithText:@"网络异常"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CarBrandModel *model = self.tableArray[indexPath.row];
    CarBrandCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarBrandCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setupCell:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CarBrandModel *model = self.tableArray[indexPath.row];
    BrandSeriesViewController *vc = [[BrandSeriesViewController alloc] init];
    vc.brandId = model.brandId;
    vc.brandName = model.name;
    vc.type = _type;
    [self.navigationController pushViewController:vc animated:YES];
}



@end
